package org.come.control;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.come.Frame.TestChildJframe;
import org.come.action.FromServerAction;
import org.come.bean.BabyListBean;
import org.come.entity.Baby;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;

import com.tool.role.RoleData;
import com.tool.role.RoleProperty;

/**
 * 获取宝宝
 * 
 * @author 黄建彬
 * 
 */

public class BabyControl implements FromServerAction {

	static ImageIcon icon1;
	static ImageIcon icon2;

	public static ImageIcon geticon(int sex) {
		if (sex == 0) {
			if (icon1 == null) {
                icon1 = new ImageIcon("inkImg/old/baby/0.png");
            }
			return icon1;
		} else {
			if (icon2 == null) {
                icon2 = new ImageIcon("inkImg/old/baby/1.png");
            }
			return icon2;
		}
	}

	@Override
	public void controlMessFromServer(String mes, String type) {
		BabyListBean babyListBean = GsonUtil.getGsonUtil().getgson().fromJson(mes, BabyListBean.class);
		List<Baby> babyList = babyListBean.getBabyList();
		babyinit(babyList);
		FormsManagement.showForm(1);
	}

	/** 宝宝初始化 */
	public static void babyinit(List<Baby> babyList) {
		// 保存到静态类里面
		if (babyList == null && UserMessUntil.getMyListBaby() != null) {
            return;
        }
		BigDecimal big = RoleData.getRoleData().getLoginResult().getBabyId();
		if (big == null) {
			big = new BigDecimal(-1);
		}
		if (babyList == null) {
			babyList = new ArrayList<>();
		}
		UserMessUntil.setMyListBaby(babyList);
		JLabel[] jLabels = TestChildJframe.getTestChildJframe().getTestChildJpanel().getLabListChild();
		for (int i = 0; i < 6; i++) {
			Baby baby = null;
			if (babyList.size() > i) {
                baby = babyList.get(i);
            }
			if (baby != null) {
				jLabels[i].setIcon(geticon(baby.getChildSex()));
				if (big.compareTo(baby.getBabyid()) == 0) {
					RoleProperty.ResetBaby(baby);
				}
			} else {
				jLabels[i].setIcon(null);
			}
		}
		if (babyList.size() == 0) {
			TestChildJframe.getTestChildJframe().getTestChildJpanel().ShowBaby(null);
		} else {
			TestChildJframe.getTestChildJframe().getTestChildJpanel().ShowBaby(babyList.get(0));
		}
	}
}
